package net.realsee.service.wcs.work.impl;

import cn.hutool.core.bean.BeanUtil;
import net.realsee.common.utils.StringUtils;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.framework.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import net.realsee.domain.wcs.work.bo.WcsWarnStatusBo;
import net.realsee.domain.wcs.work.vo.WcsWarnStatusVo;
import net.realsee.domain.wcs.work.WcsWarnStatus;
import net.realsee.mapper.wcs.work.WcsWarnStatusMapper;
import net.realsee.service.wcs.work.IWcsWarnStatusService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 告警状态定义Service业务层处理
 *
 * @author realsee
 * @date 2023-08-24
 */
@RequiredArgsConstructor
@Service
public class WcsWarnStatusServiceImpl extends ServiceImpl<WcsWarnStatusMapper, WcsWarnStatus> implements IWcsWarnStatusService {

    private final WcsWarnStatusMapper baseMapper;

    /**
     * 查询告警状态定义
     */
    @Override
    public WcsWarnStatusVo selectVoById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询告警状态定义列表
     */
    @Override
    public TableDataInfo<WcsWarnStatusVo> selectVoPage(WcsWarnStatusBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsWarnStatus> lqw = buildVoQueryWrapper(bo);
        Page<WcsWarnStatusVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询告警状态定义列表
     */
    @Override
    public List<WcsWarnStatusVo> selectVoList(WcsWarnStatusBo bo) {
        LambdaQueryWrapper<WcsWarnStatus> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsWarnStatus> buildVoQueryWrapper(WcsWarnStatusBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsWarnStatus> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getWarnStatus()), WcsWarnStatus::getWarnStatus, bo.getWarnStatus());
        lqw.like(StringUtils.isNotBlank(bo.getStatusName()), WcsWarnStatus::getStatusName, bo.getStatusName());
        return lqw;
    }

    /**
     * 新增告警状态定义
     */
    @Override
    public Boolean insertByBo(WcsWarnStatusBo bo) {
        WcsWarnStatus add = BeanUtil.toBean(bo, WcsWarnStatus.class);
        return this.save(add);
    }

    /**
     * 修改告警状态定义
     */
    @Override
    public Boolean updateByBo(WcsWarnStatusBo bo) {
        WcsWarnStatus update = BeanUtil.toBean(bo, WcsWarnStatus.class);
        return this.updateById(update);
    }

    /**
     * 批量删除告警状态定义
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
